package com.gamex.findit.controller.web;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.gamex.findit.Constants;
import com.gamex.findit.model.CommonCode;
import com.gamex.findit.model.Image;
import com.gamex.findit.model.ImageCut;
import com.gamex.findit.service.CommonCodeService;
import com.gamex.findit.service.GameItemService;
import com.gamex.findit.service.ImageService;
import com.gamex.findit.service.SkillService;
import com.gamex.findit.util.CommonUtil;

@Controller
public class CommonController {
	
	private static final Logger logger = LoggerFactory.getLogger(CommonController.class);
	
	@Autowired ImageService imageService;
	@Autowired CommonCodeService commonCodeService;
	@Autowired GameItemService gameItemService;
	@Autowired SkillService skillService;
	 
	/**
	 * @param url
	 * @param page
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/admin/{url}/go{page}.*")	
	public String goUrl(@PathVariable String url,@PathVariable String page, Model model) throws Exception{
		try {
		
			if(page.equals("writeImage")){
				//
			}else if(page.equals("writeItem")){
				CommonCode code = new CommonCode();
				code.setCodeGroup("ITEM_TYPE");
				model.addAttribute("itemTypes",commonCodeService.getCommonCode(code));
			}else if(page.equals("writeMemberItem")){
				model.addAttribute("itemNos",gameItemService.getGameItemNo());
			}else if(page.equals("writeMemberSkill")){
				model.addAttribute("skillIds",skillService.getSkillIds());
			}
			
		} catch (Exception e) {
			// TODO: handle exception
		}
			return "/" +url+ "/" +page;
	}
	
	/**
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/image/member/writeImage.*")	
	public String goUrl2(Model model) throws Exception{
			return "/image/member/writeImage";
	}
	
	/**
	 * @param imgId
	 * @param request
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/common/prevImage.*")	
	public String prevImage(@RequestParam("imgId") int imgId,
							 HttpServletRequest request,
							 Model model) throws Exception{
						
		Image image = new Image();
		image.setImgId(imgId);
		
		Image original = imageService.getImageView(image); 
		
		String imgPaht = original.getImg();
		String imgName = imgPaht.substring(imgPaht.lastIndexOf("/") + 1, imgPaht.length()) ;
		String originalImgPath = CommonUtil.getRealPath(request, Constants.DEFAULT_IMG_PATH, imgName);
		String tmpImgName = CommonUtil.getRealPath(request, Constants.DEFAULT_TMP_PATH, imgName);
		
		FileUtils.copyFile(new File(originalImgPath), new File(tmpImgName));
		Image tmpImage = original;
		List<ImageCut> imageCuts = new ArrayList<ImageCut>();
		
		imageCuts.add(imageService.getImageCut(image.getImgId(), 1));
		imageCuts.add(imageService.getImageCut(image.getImgId(), 2));
		imageCuts.add(imageService.getImageCut(image.getImgId(), 3));
		imageCuts.add(imageService.getImageCut(image.getImgId(), 4));
		imageCuts.add(imageService.getImageCut(image.getImgId(), 5));
		imageCuts.add(imageService.getImageCut(image.getImgId(), 6));
		imageCuts.add(imageService.getImageCut(image.getImgId(), 7));
		imageCuts.add(imageService.getImageCut(image.getImgId(), 8));
		imageCuts.add(imageService.getImageCut(image.getImgId(), 9));
		imageCuts.add(imageService.getImageCut(image.getImgId(), 10));
		
		tmpImage.setImg(tmpImgName);
		
		CommonUtil.assambleImage(request, tmpImage, imageCuts);
		
		model.addAttribute("original", Constants.DEFAULT_IMG_PATH + imgName);
		model.addAttribute("tmpImg", Constants.DEFAULT_TMP_PATH + imgName);
		
	return "/common/prevImage";
	
	}
	
	/**
	 * @param tmpImg
	 * @param request
	 * @throws Exception
	 */
	@RequestMapping("/common/deleteTmpImg.*")	
	public void prevImage(@RequestParam("tmpImg") String tmpImg,
			              HttpServletRequest request) throws Exception{
			File file = new File(CommonUtil.getRealPath(request, tmpImg,""));
			if(file.exists()){
				file.delete();
			}
	}
	
	
	
}
